package com.wugh.fast.security.core.util;

import com.google.common.collect.Lists;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 说明:
 *
 * @author wugh
 * @date 2018-10-18 17:37
 */
public class AuthorityUtils {
    public static Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) {
        Object authorities = map.get("authorities");
        if (authorities == null) {
            return Lists.newArrayList();
        }
        if (authorities instanceof String) {
            return org.springframework.security.core.authority.AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);
        }
        if (authorities instanceof Collection) {
            return org.springframework.security.core.authority.AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils
                    .collectionToCommaDelimitedString((Collection<?>) authorities));
        }
        throw new IllegalArgumentException("Authorities must be either a String or a Collection");
    }
}
